import axios from 'axios'
import { BaseService } from './BaseService'
import { message } from 'antd'

export class UserService extends BaseService {
    // get
    // 获取用户的详细信息、点赞图片、上传图片
    // type:profile | likes | pictures
    public async getProfile(userId: string) {
        let url = this.usersBaseUrl + '/' + userId + '/profile'
        return await axios.get(url)
    }

    public async getPictures(userId: string, p: number, n: number) {
        let url = this.usersBaseUrl + '/' + userId + '/pictures?p=' + p + '&n=' + n
        return await axios.get(url)
    }

    public async getLikes(userId: string, p: number, n: number) {
        let url = this.usersBaseUrl + '/' + userId + '/likes?p=' + p + '&n=' + n
        return await axios.get(url)
    }

    // post
    // 上传用户的点赞的图片、发布的图片
    // type:likes | pictures
    public async postLike(userId: string, date: any) {
        const url = this.usersBaseUrl + '/' + userId + '/likes'
        await axios
            .post(url, date)
            .then(res => {
                this.success()
            })
            .catch(err => {
                this.failed(err)
            })
    }

    public async postPicture(userId: string, date: any) {
        const url = this.usersBaseUrl + '/' + userId + '/pictures'
        await axios
            .post(url, date)
            .then(res => {
                message.success('上传成功。')
            })
            .catch(err => {
                message.error('上传失败。')
            })
    }

    // put
    // 修改用户信息、图片基本信息
    // type:profile | pictures
    public async updateProfile(userId: string, date: any) {
        const url = this.usersBaseUrl + '/' + userId + '/profile'
        await axios
            .put(url, date)
            .then(res => {
                this.success()
                console.log(res.data.data)
            })
            .catch(err => {
                this.failed(err)
            })
    }

    public async updatePicture(userId: string, date: any, pictureId?: string) {
        const url = this.usersBaseUrl + '/' + userId + '/pictures/' + pictureId
        await axios
            .put(url, date)
            .then(res => {
                this.success()
            })
            .catch(err => {
                console.log(err)
            })
    }

    // public put(userId: string, type: string, date: any, pictureId?: string) {
    //     const url = this.usersBaseUrl + '/' + userId + '/' + type + '/' + pictureId
    //     axios
    //         .put(url, date)
    //         .then(res => {
    //             this.success()
    //         })
    //         .catch(err => {
    //             this.failed(err)
    //         })
    // }

    // delete
    // 删除用户点赞图片，用户上传图片
    // type:likes | pictures
    public async deletePictures(userId: string, pictureId: string) {
        const url = this.usersBaseUrl + '/' + userId + '/pictures/' + pictureId
        return axios.delete(url)
    }
    public async deleteLikes(userId: string, pictureId: string) {
        const url = this.usersBaseUrl + '/' + userId + '/likes/' + pictureId
        await axios
            .delete(url)
            .then(res => {
                this.success()
            })
            .catch(err => {
                this.failed(err)
            })
    }
    // public delete(userId: string, type: string, pictureId: string) {
    //     const url = this.usersBaseUrl + '/' + userId + '/' + type + '/' + pictureId
    //     axios
    //         .delete(url)
    //         .then(res => {
    //             this.success()
    //         })
    //         .catch(err => {
    //             this.failed(err)
    //         })
    // }
}
